package com.vitabuybuy.tv.dialog

import android.content.Context
import android.view.View
import android.view.animation.Animation
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.library.common.util.DensityUtil
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ItemResonBinding
import com.vitabuybuy.tv.databinding.PopupRefundeResonBinding
import com.library.common.base.BindRecycAdapter
import com.vitabuybuy.tv.ui.mine.order.after.RequestRefundActivity
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig

/**
 * 作者：SimGa
 * 日期：2021/1/15
 * 时间：15:55
 * 包名：com.vitabuybuy.tv.dialog
 */
class RefundResonDialog(
    context: Context,
    returnType: Int,
    val onConfirm: (reson: String) -> Unit
) : BasePopupWindow(context) {

    private lateinit var mBinding: PopupRefundeResonBinding

    private lateinit var mAdapter: ResonAdater

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_refunde_reson)
    }

    init {
        val resonlist = ArrayList<String>()
        when (returnType) {
            RequestRefundActivity.AFTER_SALE_TYPE_BY_ONLY_RETUAN_MONEY -> {
                resonlist.addAll(context.resources.getStringArray(R.array.only_return_money_reson_list))
            }
            RequestRefundActivity.AFTER_SALE_TYPE_BY_RETURN_GOODS_AND_RETUAN_MONEY -> {
                resonlist.addAll(context.resources.getStringArray(R.array.return_goods_and_money_reson_list))
            }
        }
        mAdapter.setNewData(resonlist)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        mBinding = DataBindingUtil.bind<ViewDataBinding>(contentView) as PopupRefundeResonBinding


        mAdapter = ResonAdater()
        mBinding.listview.apply {
            adapter = mAdapter
            addItemDecoration(RecycleViewDivider(context, DensityUtil.dip2px(1f)))
            layoutManager = LinearLayoutManager(context)
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            mAdapter.notifyItemChanged(mAdapter.checkPos)
            mAdapter.checkPos = position
            mAdapter.notifyItemChanged(mAdapter.checkPos)
        }
        mBinding.ivClose.setOnClickListener {
            dismiss()
        }
        mBinding.btnConfirm.setOnClickListener {
            onConfirm.invoke(mAdapter.mData[mAdapter.checkPos])
            dismiss()
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.FROM_BOTTOM)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.TO_BOTTOM)
            .toDismiss()
    }

    class ResonAdater : BindRecycAdapter<ItemResonBinding, String>() {
        var checkPos = 0
        override fun onBindViewHolder(
            mBinding: ItemResonBinding,
            itemData: String,
            holder: RecyclerView.ViewHolder,
            position: Int
        ) {
            mBinding.reson = itemData
            mBinding.isCheck = checkPos == position
        }
        override fun normalViewId(viewType: Int): Int {
            return R.layout.item_reson
        }
    }
}